package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.service.CrUnifiedInventoryService;
import com.epoch.app.otsapplicationappsdk.inventorydetail.dto.RenderRequest;
import com.epoch.app.otsapplicationappsdk.inventorydetail.service.InventoryDetailService;
import com.epoch.app.crunifiedinventory.dto.request.AloocateOrderGetTransRequest;
import com.epoch.app.crunifiedinventory.model.dto.AloocateOrderGetTransResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InventoryDetailV2ServiceImpl implements InventoryDetailService {

//    @Autowired
//    private OtsUnifiedInventoryService otsUnifiedInventoryService;
    @Autowired
    private CrUnifiedInventoryService otsUnifiedInventoryService;

    private static Log log = Log.getLogger(InventoryDetailV2ServiceImpl.class);

    @Override
    public Object render(RenderRequest renderRequest) {
        JSONObject result = new JSONObject();
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(renderRequest.getAttr()));
        String receiveAreaId = jsonObject.getString("receiveAreaId");
        String scItemId = jsonObject.getString("scItemId");
        try {
            AloocateOrderGetTransRequest request = new AloocateOrderGetTransRequest();
            request.setReceiveAreaId(receiveAreaId);
            request.setCode("");
            request.setScItemId(scItemId);
            log.info("render_otsUnifiedInventoryService.getTransOrders$request:" + JSON.toJSONString(request));
            Result<List<AloocateOrderGetTransResponse>> response = otsUnifiedInventoryService.getTransOrders(request);
            log.info("render_otsUnifiedInventoryService.getTransOrders$response:" + JSON.toJSONString(response));
            result.put("itemList", response.getResult());
            result.put("success", CollectionUtils.isNotEmpty(response.getResult()));
        }catch (Exception e){
            result.put("success", false);
            result.put("msg",e.getMessage());
        }
        return result;
    }
}
